package com.gxnu.edu.cn.client.ui;

import com.gxnu.edu.cn.client.controller.UserClient;
import com.gxnu.edu.cn.ttxxt.entity.Users;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import java.time.LocalDateTime;

@Component
public class RegisterFrame extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JTextField phoneField;
    private JTextField emailField;
    private JTextField avatarUrlField;
    private JTextArea signatureArea;

    private UserClient client;

    public RegisterFrame(UserClient client) {
        this.client = client;
        setTitle("即时通讯软件 - 注册");
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        JLabel titleLabel = new JLabel("注册新用户");
        titleLabel.setFont(new Font("SimHei", Font.BOLD, 24));
        titleLabel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
        mainPanel.add(titleLabel);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));

        JPanel inputPanel = new JPanel(new GridLayout(7, 2, 10, 10));
        inputPanel.add(new JLabel("用户名:"));
        usernameField = new JTextField();
        inputPanel.add(usernameField);
        inputPanel.add(new JLabel("密码:"));
        passwordField = new JPasswordField();
        inputPanel.add(passwordField);
        inputPanel.add(new JLabel("手机号:"));
        phoneField = new JTextField();
        inputPanel.add(phoneField);
        inputPanel.add(new JLabel("邮箱:"));
        emailField = new JTextField();
        inputPanel.add(emailField);
        inputPanel.add(new JLabel("头像URL:"));
        avatarUrlField = new JTextField();
        inputPanel.add(avatarUrlField);
        inputPanel.add(new JLabel("个性签名:"));
        signatureArea = new JTextArea(3, 20);
        inputPanel.add(new JScrollPane(signatureArea));
        mainPanel.add(inputPanel);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));

        JPanel buttonPanel = new JPanel();
        JButton registerButton = new JButton("注册");
        registerButton.addActionListener(e -> {
            String username = usernameField.getText();
            String password = new String(passwordField.getPassword());
            String phone = phoneField.getText();
            String email = emailField.getText();
            String avatarUrl = avatarUrlField.getText();
            String signature = signatureArea.getText();

            Users users = new Users();
            users.setUsername(username);
            users.setPasswordHash(password);
            users.setPhone(phone);
            users.setEmail(email);
            users.setAvatarUrl(avatarUrl);
            users.setSignature(signature);
            users.setCreateTime(LocalDateTime.now());
            users.setUpdateTime(LocalDateTime.now());

            // 调用注册方法
            client.register(users).whenComplete((result, ex) -> {
                SwingUtilities.invokeLater(() -> {
                    if (ex != null) {
                        JOptionPane.showMessageDialog(RegisterFrame.this,
                                "注册失败: " + ex.getMessage(),
                                "错误",
                                JOptionPane.ERROR_MESSAGE);
                        return;
                    }

                    if (result != null && result.containsKey("成功")) {
                        JOptionPane.showMessageDialog(RegisterFrame.this,
                                "注册成功，请登录",
                                "注册提示",
                                JOptionPane.INFORMATION_MESSAGE);
                    } else {
                        JOptionPane.showMessageDialog(RegisterFrame.this,
                                result != null ? result : "注册失败，未知错误",
                                "注册提示",
                                JOptionPane.ERROR_MESSAGE);
                    }
                });
            });
        });
        buttonPanel.add(registerButton);
        mainPanel.add(buttonPanel);

        add(mainPanel);
    }

    public void display() {
        SwingUtilities.invokeLater(() -> {
            setVisible(true);
        });
    }
}